<template>
   <div id='login' class="login_container">
     <div class="login_box">
       <!-- 头像 -->
       <div class="avatar_box">
         <img src="../assets/logo.png" alt=""/>
       </div>
       <!-- 登录表单 -->
       <el-form class="login_form" :model="loginForm" :rules="loginRules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="el-icon-lock" v-model="loginForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetData">重置</el-button>
        </el-form-item>
       </el-form>
     </div>
   </div>
</template>
<script>
 export default {
   name:'login',
   data () {
     return {
       loginForm:{
         username: 'admin',
         password: '123456'
       },
      //  登录时的校验规则
       loginRules: {
         username: [
           {required: true, message:'请输入用户名', trigger: 'blur'},
         ],
         password: [
           {required: true, message:'请输入密码', trigger: 'blur'}
         ]
       }
     }
   },
   mounted() {
   },
   methods: {
    //  重置数据
     resetData(){
       this.$refs.loginFormRef.resetFields()
     },
    //  登录
     login(){
      //  判断验证规则
       this.$refs.loginFormRef.validate(async (valid) => {
         if (valid) {
           this.$http.post('login',this.loginForm).then(res => {
             if(res.data.meta.status == 200){
              //  将登录成功之后的token保存到sessionStorage中
              // 项目中除登录之外的其他API接口，必须在登录之后才能访问
               window.sessionStorage.setItem('token',res.data.data.token)
               this.$message({
                 message: res.data.meta.msg,
                 type: 'success',
                 duration: 600
               })
               this.$router.push('/home')
             }else{
               return this.$message({
                 message: res.data.meta.msg,
                 type: 'error',
                 duration: 2000
               })
             }
           })
         }
       })
     }
   },
   components: {
   }
 }
</script>
<style scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #ffffff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top:50%;
  transform: translate(-50%,-50%);/*translate(-50%,-50%) 作用是，往上（x轴）,左（y轴）移动自身长宽的 50%，以使其居于中心位置。*/
}
.avatar_box {
  height: 130px;
  width: 130px;
  border:1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%,-50%);
  background-color: #fff;
}
.avatar_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding:  0 20px;
  box-sizing: border-box;
}
.btns{
  display: flex;
  justify-content: flex-end;
}
</style>
